import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

private int[] longest(String s, int i, int j) {
		if (i == j) {
			int[] res = new int[2];
			res[0] = i;
			res[1] = i;
			return res;
		}
		int[] s1 = longest(s, i + 1, j);
		if (s1[0] == i+1) {
			if (!hasRepetition(s, i, s1[1], 1)) {
				s1[0] = i;
			}
		}
		if (len(s1) == 1 && s.charAt(i) != s.charAt(i+1)){
			s1[0] = i;
			s1[1] = i+1;
		}
		
		int[] s2 = longest(s, i, j - 1);
		if (s2[1] == j - 1) {
			if (!hasRepetition(s, s2[0], j, 0)) {
				s2[1] = j;
			}
		}
		if (len(s2) == 1 && s.charAt(j - 1) != s.charAt(j)){
			s2[0] = j - 1;
			s2[1] = j;
		}
		
		if (len(s2) > len(s1)) {
			return s2;
		}
		else {
			return s1;
		}
	}

	private int len(int[] s1) {
		return s1[1] - s1[0] + 1;
	}
	
	Map<List<Integer>, Boolean> hasRepetition = new HashMap<List<Integer>, Boolean>();

	private boolean hasRepetition(String s, int i, int j, int isLeft) {
		List<Integer> key = Arrays.asList(i, j, isLeft);
		Boolean has = hasRepetition.get(key);
		if (has != null) {
			return has;
		}
		has = false;
//		System.out.println(String.format("(%s %s)", i,j));
		if (isLeft == 1) {
			char c = s.charAt(i);
			for (int k = i+1; k < j+1; k++) {
				if (c == s.charAt(k)) {
					has = true;
					break;
				}
			}
		}
		else {
			char c = s.charAt(j);
			for (int k = j - 1; k >= i; k--) {
				if (c == s.charAt(k)) {
					has = true;
					break;
				}
			}
		}
		hasRepetition.put(key, has);
		return has;
	}